package com.service.acl.controller;


import com.service.acl.pojo.Permission;
import com.service.acl.pojo.User;
import com.service.acl.service.PermissionService;
import com.service.utils.ResultVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-22
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService PermissionService;



    @GetMapping("queryAll")
    @ApiOperation("查询所有的管理员用户权限信息")
    public ResultVo list(){

        List<Permission> list = PermissionService.queryAll();
        return ResultVo.ok().data("items",list);
    }

    @GetMapping("queryRoleId/{roleId}")
    @ApiOperation("根据角色id查询所对应的权限信息")
    public ResultVo queryRoleId(String roleId){
        List<Permission> permissions = PermissionService.queryRoleId(roleId);
        return ResultVo.ok().data("items",permissions);
    }

    /**
     * 查询指定的管理员用户权限信息
     * @param id
     * @return
     */
    @ApiOperation("根据上级权限id查询其下级权限的所有信息")
    @GetMapping("queryChildren/{id}")
    public  ResultVo queryById(@ApiParam(name = "id",value = "指定查询的上级权限ID",required = true) @PathVariable String id){
        List<Permission> permissions = PermissionService.queryChildren(id);
        return ResultVo.ok().data("item",permissions);
    }

    /**
     * 删除指定的管理员用户权限信息
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除管理员用户权限信息")
    @DeleteMapping("{id}")
    public ResultVo delete(@ApiParam(name = "id",value = "逻辑删除的管理员用户权限ID",required = true) @PathVariable String id){
       PermissionService.delete(id);
            return ResultVo.ok();

    }

    /**
     * 修改管理员用户权限信息
     * @param permission
     * @return
     */
    @ApiOperation("修改管理员用户权限信息")
    @PutMapping
    public ResultVo update(@RequestBody Permission permission){
        permission.setGmtModified(new Date());
        boolean b = PermissionService.updateById(permission);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 添加管理员用户权限信息
     * @param permission
     * @return
     */
    @ApiOperation("添加管理员用户权限信息")
    @PostMapping
    public ResultVo save(@RequestBody Permission permission){
        permission.setGmtModified(new Date());
        permission.setGmtCreate(new Date());
        boolean b = PermissionService.save(permission);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

}

